package com.blogs.service;

import java.util.ArrayList;



import com.blogs.entity.AppConfig;
import com.blogs.entity.IngFeed;
import com.blogs.entity.MsgEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class GetIngLine {
	public interface IMsgCallBack {
		public void onSuccess(ArrayList<IngFeed> feedList);

		public void onFailed();
	}
	private String url = AppConfig.GET_ING_LINE;
	private IMsgCallBack callback;
	private GetMsgTask timeLine;
	public GetIngLine(String sid, int page,String since_id,String max_id, IMsgCallBack callback) {
		this.callback = callback;
		timeLine = new GetMsgTask(taskCallBack);
		url = url.replace("{0}", sid).replace("{1}", page + "");
	}
	public void loadData() {
		timeLine.execute(url);
	}
	private GetMsgTask.onTaskCallBack taskCallBack = new GetMsgTask.onTaskCallBack() {

		public void onSuccess(String result) {
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<ArrayList<IngFeed>> msg=null;
			try
			{
				msg=gson.fromJson(result,new TypeToken<MsgEntity<ArrayList<IngFeed>>>(){}.getType());
			}
			catch(Exception e)
			{
				callback.onFailed();
				return;
			}
			if(msg.op.equals(""))
			{
				callback.onFailed();
				return;
			}
			callback.onSuccess(msg.data);
		}
		public void onFailed() {
			// TODO Auto-generated method stub
			callback.onFailed();
		}
	};
}
